package com.qiaofang.applet.service.other.util;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class DateTypeUtil {

    public static final String DATE_TYPE_1 = "1"; //今天

    public static final String DATE_TYPE_2 = "2"; //昨天

    public static final String DATE_TYPE_3 = "3"; //本周

    public static final String DATE_TYPE_4 = "4"; //本月


    public static Map<String, Date> stringToDate(Map<String, String> map) {
        Map<String, Date> dateMap = new HashMap<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String startTime = map.get("beginDate");
        String endTime = map.get("endDate");
        try {
            Date stTime = sdf.parse(startTime);
            dateMap.put("beginDate", stTime);
            Date enTime = sdf.parse(endTime);
            dateMap.put("endDate", enTime);
        } catch (Exception e) {
        }
        return dateMap;
    }


    /**
     * 指定日期前后时间
     *
     * @param date 指定日期 Calendar.MINUTE
     * @param type Calendar类型
     * @return
     */
    public static Date getBeginOrAfterDay(Date date, int type, int num) {
        Calendar cl = Calendar.getInstance();
        cl.setTime(date);
        cl.add(type, num);
        return cl.getTime();
    }

    /**
     * 时间戳转String
     *
     * @param timestamp
     * @return
     */
    public static String timeStamp2Date(Long timestamp) {
        String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(timestamp));
        return date;
    }

    /**
     * 计算传入时间与当前时间差
     *
     * @param lastDate
     * @return 秒
     */
    public static long countdown(Date lastDate) {
        long getTime = System.currentTimeMillis() - lastDate.getTime();
        return getTime / 1000;
    }

    /**
     * 计算两个日期bai之间相差的天数
     *
     * @param smdate 较小的时du间
     * @param bdate  较大的时间
     * @return 相差天数
     * @throws ParseException
     */
    public static int daysBetween(Date smdate, Date bdate) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        smdate = sdf.parse(sdf.format(smdate));
        bdate = sdf.parse(sdf.format(bdate));
        Calendar cal = Calendar.getInstance();
        cal.setTime(smdate);
        long time1 = cal.getTimeInMillis();
        cal.setTime(bdate);
        long time2 = cal.getTimeInMillis();
        long between_days = (time2 - time1) / (1000 * 3600 * 24);
        return Integer.parseInt(String.valueOf(between_days));
    }

    public static String getWorkAgeStr(Date attendWorkTime) {
        String workAge;
        if (attendWorkTime == null) {
            workAge = "";
        } else {
            Calendar begin = Calendar.getInstance();
            Calendar now = Calendar.getInstance();
            begin.setTime(attendWorkTime);
            now.setTime(new Date());
            int dayGap = now.get(Calendar.DATE) - begin.get(Calendar.DATE);
            int monthGap = now.get(Calendar.MONTH) - begin.get(Calendar.MONTH);
            int yearGap = now.get(Calendar.YEAR) - begin.get(Calendar.YEAR);
            if (yearGap > 0) {
                if (monthGap < 0 || (monthGap == 0 && dayGap < 0)) {
                    yearGap--;
                }
            }
            workAge = yearGap < 1 ? "少于1年" : "从业" + yearGap + "年";
        }
        return workAge;
    }

    /**
     * 获取历史上的今天
     *
     * @param prevNum
     * @return
     */
    public static Date getTodayInHistory(int prevNum) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        calendar.set(Calendar.YEAR, calendar.get(Calendar.YEAR) - prevNum);
        return calendar.getTime();
    }

    public static Date getDay8am(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 8);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    public static Date getDayZero(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    public static Date getTomorrow(Date date) {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, 1);
        return calendar.getTime();
    }

    public static Date getYesterDay(Date date) {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, -1);
        return calendar.getTime();
    }

}
